package com.simba.server.task;

import com.simba.server.components.ConfigManager;
import org.apache.log4j.Logger;
import org.jboss.netty.channel.Channel;
import com.simba.common.frameserver.FrameChannelManager;
import com.simba.common.log.LogUtil;
import com.simba.server.components.MachineParams;
import com.simba.server.components.SlotManager;
import com.simba.server.components.SortProtocolCreator;

public class SorterStatusTask extends Thread
{
	private final static Logger logger = Logger.getLogger(SorterStatusTask.class);
	private static final int REQUEST_INTERVAL = 2000;

	public SorterStatusTask()
	{
		this.setName("Sorter-Status-Task");
	}

	@Override
	public void run()
	{
		while (!Thread.currentThread().isInterrupted())
		{
			try
			{
				this.reloadBasicConfig();
				this.sendSlotSortLockStatus();
				Thread.sleep(SorterStatusTask.REQUEST_INTERVAL);
			}
			catch (Exception ex)
			{
				if (ex instanceof InterruptedException)
				{
					Thread.currentThread().interrupt();
				}
				LogUtil.LogError(SorterStatusTask.logger,
						"Catch an exception [" + ex.getMessage() + "]");
			}
		}
	}

	private void reloadBasicConfig()
	{
		ConfigManager.getInstance().loadBasicConfig();
	}


	private void sendSlotSortLockStatus()
	{
		try
		{
//			byte[] slotLockStatus = SlotManager.getInstance().getSlotLockStatus();
//			byte[] replyBuffer = SortProtocolCreator.getInstance().createSlotSoftLockStatus(slotLockStatus);
//
//			Channel[] channels = FrameChannelManager.getInstance().getChannelList();
//			for (Channel tmpChannel : channels)
//			{
//				FrameChannelManager.getInstance().commitResponse(tmpChannel, replyBuffer, false);
//			}
		}
		catch (Exception ex)
		{
			LogUtil.LogError(SorterStatusTask.logger,
					"[SorterStatusTask.sendMachineParams] Catch an exception [" + ex.getMessage()
							+ "]");
		}
	}
}
